package com.ansion.commonchat.startup.workmanager.bwork.impl.mode;

import android.text.TextUtils;

import com.ansion.commonchat.startup.workmanager.bwork.BWorkInfo;
import com.ansion.log.LogTag;
import com.ansion.log.VLog;
import com.ansion.utils.GsonUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wanghongjie on 2023/7/18
 * Describe :  API 实现类，这是自定义的一个Dao 实现类， WorkManager 并不存在。
 * <p>
 * 用于记录哪些 work 被监听，存储被监听的work及监听者，目前仅用于状态变更时，遍历通知对应监听者。
 * <p>
 * 存储、通知， 后续待扩展...
 */
public class BWorkCallbackImpl implements BWorkCallbackDao {
    private WorkDataCache workDataCache;
    private final Object mLock = new Object();

    private final Map<Long, BWorkCallback> callbacksMap = new HashMap<>();

    public BWorkCallbackImpl(WorkDataCache workDataCache) {
        this.workDataCache = workDataCache;
    }

    @Override
    public void insertWorkCallback(BWorkCallback workCallback) {
        synchronized (mLock) {
            callbacksMap.put(workCallback.listener.getListenerId(), workCallback);
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_B_WORK_MANAGER, " BWorkCallbackDao insertWorkCallback workListenerId %s , workCallback >>   %s , totalSize >> %s ", workCallback.listener.getListenerId(), GsonUtil.GsonString(workCallback), callbacksMap.size());
            }
        }
    }

    @Override
    public void deleteWorkCallback(long workListenerId) {
        synchronized (mLock) {
            BWorkCallback callback = callbacksMap.remove(workListenerId);
            if (VLog.OPEN_LOG) {
                VLog.d(LogTag.TAG_B_WORK_MANAGER, " BWorkCallbackDao deleteWorkCallback workListenerId >>   %s , callback is removed >> %s , totalSize >> %s ", workListenerId, GsonUtil.GsonString(callback), callbacksMap.size());
            }
        }
    }

    @Override
    public void notifyWorkStateChange(String workId) {
        Map<Long, BWorkCallback> copyMap = new HashMap<>(callbacksMap);
        if (copyMap.size() > 0 && !TextUtils.isEmpty(workId)) {
            for (long key : copyMap.keySet()) {
                BWorkCallback callback = copyMap.get(key);
                if (callback != null && callback.workIds.contains(workId)) {
                    BWorkSpec.BWorkInfoPojo infoPojo = workDataCache.workSpecDao().getWorkStatusPojoForId(workId);
                    if (infoPojo != null) {
                        BWorkInfo info = infoPojo.toWorkInfo();
                        if (VLog.OPEN_LOG) {
                            VLog.d(LogTag.TAG_B_WORK_MANAGER, " BWorkCallbackDao notifyWorkStateChange , listenerId >> %s,  workId >>   %s , info >> %s , totalSize >> %s ", callback.listener.getListenerId(), workId, GsonUtil.GsonString(info), copyMap.size());
                        }
                        callback.listener.onWorkStateChange(callback.listener.getListenerId(), workId, info);
                    }
                }
            }
        }
    }

    @Override
    public void logout() {
        synchronized (mLock) {
            callbacksMap.clear();
            VLog.d(LogTag.TAG_B_WORK_MANAGER, " BWorkCallbackDao logout");
        }
    }
}
